package com.holike.crm.fragment.monthdata;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.holike.crm.R;
import com.holike.crm.adapter.AbsFormAdapter;
import com.holike.crm.base.BaseActivity;
import com.holike.crm.base.BaseFragment;
import com.holike.crm.bean.MonthDataDesignBean;
import com.holike.crm.helper.FormDataHelper;

import java.util.List;


class DesignManagerMonthDataHelper extends MonthDataHelper {

    private FrameLayout mContainerLayout;

    DesignManagerMonthDataHelper(BaseFragment<?, ?> fragment, Callback callback) {
        super(fragment, callback);
    }

    @Override
    void initView() {
        mContainerLayout = mFragmentView.findViewById(R.id.fl_container);
    }

    void onSuccess(MonthDataDesignBean bean) {
        mContainerLayout.removeAllViews();
        View view = LayoutInflater.from(mContext).inflate(R.layout.include_month_data_designmanager_content, mContainerLayout, true);
        TextView tvDatetime = view.findViewById(R.id.tv_datetime);
        tvDatetime.setText(bean.timeDetail);
        TextView tvShop = view.findViewById(R.id.tv_shop_detail);
        if (!TextUtils.isEmpty(bean.shopDetail)) {
            tvShop.setText(bean.shopDetail);
            tvShop.setVisibility(View.VISIBLE);
        } else {
            tvShop.setVisibility(View.GONE);
        }
        ViewStub vs = view.findViewById(R.id.vs_content);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) vs.getLayoutParams();
        lp.leftMargin = mContext.getResources().getDimensionPixelSize(R.dimen.dp_10);
        vs.setLayoutResource(R.layout.include_form_data_content);
        View contentView = vs.inflate();
        requestUpdate(bean, contentView);
    }

    private void requestUpdate(final MonthDataDesignBean bean, View contentView) {
        final boolean isFinal = bean.isFinal();
        FormDataHelper.attachView(contentView, new FormDataHelper.FormDataBinder() {
            @Override
            public int bindFirstColumnLayoutRes() {
                return R.layout.include_form_data_column_60dp;
            }

            @Override
            public CharSequence bindSideTitle() {
                return isFinal ? mContext.getString(R.string.principal) : mContext.getString(R.string.division);
            }

            @Override
            public int bindFirstRowLayoutRes() {
                return R.layout.include_monthdata_designmanager_tablist;
            }

            @Override
            public RecyclerView.Adapter bindSideAdapter() {
                return new FormDataAdapter(mContext, bean.getArr(), true, isFinal);
            }

            @Override
            public RecyclerView.Adapter bindContentAdapter() {
                return new FormDataAdapter(mContext, bean.getArr(), false, isFinal);
            }
        });
    }

    void onFailure(String failReason) {
        mContainerLayout.removeAllViews();
        LayoutInflater.from(mContext).inflate(R.layout.include_empty_page, mContainerLayout, true);
        mFragment.noNetwork(failReason);
    }

    private final class FormDataAdapter extends AbsFormAdapter<MonthDataDesignBean.ArrBean> {
        int mLayoutResourceId;
        boolean isSide;
        boolean isFinal;

        FormDataAdapter(Context context, List<MonthDataDesignBean.ArrBean> dataList, boolean isSide, boolean isFinal) {
            super(context, dataList);
            this.isSide = isSide;
            this.isFinal = isFinal;
            mLayoutResourceId = isSide ? R.layout.item_month_data_designmanager_side : R.layout.item_month_data_designmanager_content;
        }

        @Override
        protected void bindViewHolder(RecyclerHolder holder, MonthDataDesignBean.ArrBean bean, int position) {
            setClickableCell(holder, position, R.id.tv_division, isSide && bean.isClickable(), view -> startFragment(bean.type, bean.cityCode));
            holder.setText(R.id.tv_division, isFinal ? bean.name : bean.area); //划分
            holder.setText(R.id.tv_number_measure, bean.scaleCount); //量尺数
            holder.setText(R.id.tv_number_drawing, bean.picCount); //出图数
            holder.setText(R.id.tv_number_signatures, bean.contractCount); //签约数
            holder.setText(R.id.tv_number_place_orders, bean.orders); //下单数
            holder.setText(R.id.tv_deposit_customers, bean.receivables); //已收款
            holder.setText(R.id.tv_contract_amount, bean.contractTotal); //成交总金额
        }

        @Override
        protected int bindView(int viewType) {
            return mLayoutResourceId;
        }
    }

    private void startFragment(String type, String cityCode) {
        ((BaseActivity<?, ?>) mContext).startFragment(new DesignManagerMonthDataFragment(), obtainBundle(type, cityCode));
    }

    private Bundle obtainBundle(String type, String cityCode) {
        android.os.Bundle bundle = new Bundle();
        bundle.putString("type", type);
        bundle.putString("cityCode", cityCode);
        bundle.putSerializable("startDate", mStartDate);
        bundle.putSerializable("endDate", mEndDate);
        bundle.putBoolean("isAnimation", true);
        return bundle;
    }

}
